<?php

class BizmanageModel extends CommonModel {
	protected $tableName = 'biz';
	protected $_validate = array(
		array('name','require',''),
		);
		
	protected $_auto	 =	 array(
		array('status','1',self::MODEL_INSERT),
            
		array('create_time','time',self::MODEL_INSERT,'function'),
		);
	
	function setBizZoneActions($bizId,$zonsList)
	{

		if(empty($zonsList)) {
			
		}
                         $where = 'id in('.$zonsList.')';
		
		 
		
                                      
		$rs = $this->db->execute('UPDATE '.$this->tablePrefix.'zone SET bizid='.$bizId.'  WHERE '.$where);
		if($result===false) {
			return false;
		}else {
                                            
                                            
                                            return true;
		}
	}
        function addBizNum($pid){
            $this->db->execute('UPDATE '.$this->tablePrefix.'zone SET biznum=biznum+1  WHERE id='.$pid);
		if($result===false) {
			return 0;
		}else {
                        $rs = $this->db->query('select biznum FROM  '.$this->tablePrefix.'zone  WHERE  id='.$pid);                    
                                            
                        return $rs[0]['biznum'];
                }
        }
        function setSID($bizid,$sid){
            
            $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'biz SET bizSid=\''.$sid.'\'  WHERE id='.$bizid);
		if($rs===false) {
			return false;
		}else {
                                            
                                            
                        return true;
                }
        }
        function setMeterStatistics($biz){
                         $str ='INSERT INTO '.$this->tablePrefix.'meter_statistics ( bizid, metertype) '
                                                .'VALUES ('.$biz.',1)';
                          $result = $this->db->execute('INSERT INTO '.$this->tablePrefix.'meter_statistics ( bizid, metertype) '
                                                .'VALUES ('.$biz.',1)');
                          $result = $this->db->execute('INSERT INTO '.$this->tablePrefix.'meter_statistics ( bizid, metertype) '
                                                .'VALUES ('.$biz.',2)');
		if($result===false) {
			return false;
		}else {
                   return True; 
                }   
        }

        function getBizZoneActions($bizId,$pid)
	{

                             $rs=nil;
                            if(empty($bizId)) {
			
		}
                                     $where =' bizid='.$bizId;  
                                    if ($pid<>0){
                                           $where  .= ' AND pid='.$pid;
                                    }
                                        $rs = $this->db->query('select  * FROM  '.$this->tablePrefix.'zone  WHERE  '.$where);

	        return $rs;


	}
          function getBizMeterActions($bizId)
	{

                             $rs=nil;
                            if(empty($bizId)) {
			
		}
                                     $where ='AND a.bizid='.$bizId;  

                                      $rs = $this->db->query('select  a.id,a.zoneid,a.meterid,a.m_price,a.metertype,b.name FROM  '.$this->tablePrefix.'biz_meter a,  '.$this->tablePrefix.'zone b WHERE a.zoneid=b.id  '.$where);

	        return $rs;


	}
         function addBizMeterInfo($data)
	{
                            //判断商网已存在的表，如果有了更新表铭牌，如果没有添加表信息


                            $where ='zoneid='.$data['zoneid'] ;       
                            $where .=' AND bizid='.$data['bizid'] ; 
                            $where .=' AND metertype='.$data['metertype'] ; 
                            

                                $result = $this->db->execute('INSERT INTO '.$this->tablePrefix.'biz_meter ( bizid, zoneid, meterid, m_price,metertype) '
                                                .'VALUES ('.$data['bizid'].','.$data['zoneid'].',\''.$data['meterid'].'\','.$data['m_price'].','.$data['metertype'].') ');
		if($result===false) {
			return false;
		}else {
                                            if ($data['metertype']==1){
                                                     $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'biz SET meternum1=meternum1+1  WHERE id='.$data['bizid']);
                                            }else{
                                                    $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'biz SET meternum2=meternum2+1  WHERE id='.$data['bizid']);
                                            }
                                               
                                                return true;
		}
                                
                 
	       


	}
                    public function cacheBiz(){
                  if (empty($id)){
                    $pid=1;
                     }
                    $rs = $this->db->query('select count(id) AS counts  FROM  '.$this->tablePrefix.'biz  WHERE status=1');        
                      foreach ($rs as $vo){
                    	$Data['count']	=$vo['counts'];
                    }
                    F('bizcount',NULL);
                    F('bizcount',$Data);
                    }
}
?>